#include "frmbar.h"
#include "ui_frmbar.h"
#include "devicebutton.h"
#include <QTime>
#include <QTimer>
#include <QImage>
#include "quihelper.h"
#include "userinfoservice.h"

FrmBar::FrmBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FrmBar)
{
    ui->setupUi(this);
    this->initForm();
}

FrmBar::~FrmBar()
{
    delete ui;
}

void FrmBar::initForm()
{
    ui->lbMap->setStyleSheet("border-image:url(:images/bg_call.jpg)");
    ui->btn1->setText("#1");
    ui->btn2->setText("#2");
    ui->btn3->setText("#3");

    btnStyelist<<ui->btnCircle<<ui->btnPolice<<ui->btnBubble;
    foreach (QPushButton *btn, btnStyelist) {
        connect(btn,SIGNAL(clicked(bool)),this,SLOT(changeStyle()));
    }

    btnColorlist<<ui->btnGreen<<ui->btnBlue<<ui->btnRed;
    foreach (QPushButton *btn, btnColorlist) {
        connect(btn,SIGNAL(clicked(bool)),this,SLOT(changeColor()));
    }

    workThread=new TimeWokerThread;
    connect(workThread,&TimeWokerThread::resultNotify,this,&FrmBar::timeNotify);
    workThread->start();

    initRegex();
}
void FrmBar::initRegex()
{
    //QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
    //ui->lineEdit->setValidator(new QRegExpValidator(regExp,this));
    QTimer *timer=new QTimer(this);
    timer->setInterval(1000);
    connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
    timer->start();

    UserInfoService *userService=new UserInfoService;
    QString userName=  userService->getCurrentUser();
    ui->lbCurrentUser->setText(userName);
    this->currentUser=userName;
}

void FrmBar::timeNotify(int sec)
{
    if(sec%5==0)
    {
        ui->lbCurrentUser->setText(QString("User:%1").arg(this->currentUser));
    }
    else
    {
        ui->lbCurrentUser->setText(QString("sec:%1").arg(sec));
        workThread->stop();
    }
}

void FrmBar::changeStyle()
{
    QPushButton *btn=(QPushButton *)sender();
    int index=btnStyelist.indexOf(btn);
    Devicebutton::ButtonStyle style=(Devicebutton::ButtonStyle)index;
    ui->btn1->setButtonStyle(style);
    ui->btn2->setButtonStyle(style);
    ui->btn3->setButtonStyle(style);
}

void FrmBar::changeColor()
{
    QPushButton *btn=(QPushButton *) sender();
    int index=btnColorlist.indexOf(btn);
    Devicebutton::ButtonColor clr=(Devicebutton::ButtonColor)index;
    ui->btn1->setButtonColor(clr);
    ui->btn2->setButtonColor(clr);
    ui->btn3->setButtonColor(clr);
}

void FrmBar::showTime()
{
    QTime time=QTime::currentTime();
    QString strTime=time.toString("HH:mm:ss");
    ui->lineEdit->setText(strTime);

}

void FrmBar::on_lineEdit_textChanged()
{
    //ui->lineEdit->setEnabled(ui->lineEdit->hasAcceptableInput());
}

void writeFile()
{
    QString fileName="test.ini";
    QFile aFile(fileName);
    if(aFile.exists(fileName))
        aFile.remove();

    if(!aFile.open(QIODevice::WriteOnly|QIODevice::Text))
        return;

    QTextStream aStream(&aFile);
    aStream<<"test123\r\n";
    aStream<<"test456";
    aFile.close();

    QDir currentDir=QDir::current();
    QString absPath=currentDir.absolutePath();
    QUIHelper::showMessageBox(absPath);
}

void FrmBar::on_btnImgLight_clicked()
{
    QImage img(":images/bg_call.jpg");
    int centerX=img.width()/2;
    int centerY=img.height()/2;
    QRgb rgb=img.pixel(centerX,centerY);
    int r=qRed(rgb);
    int g=qGreen(rgb);
    int b=qBlue(rgb);

    float bright=0.299f*r+0.587*g+0.114*b;
    QUIHelper::showMessageBox(QString("计算的图片亮度：%1").arg(bright));

    writeFile();
}

